import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';
import sys_date from '@dfeidao/atom-nodejs/msg/sys-date';

interface Message {
	productid: string;
	priority: string;
	no: string;
	name: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const no = await doc_code(action_id, session_id, systemid, 'BM0011276', '', 1);
	const c = count(action_id, session_id, systemid);
	c.prepare('auth_formgroup', { productid: msg.productid, name: msg.name });
	const [t] = await c.exec();
	if (t > 0) {
		return 'other';
	} else {
		const { exec, insert } = operate(action_id, session_id, systemid);
		const create_time = await sys_date(action_id, session_id);
		insert('auth_formgroup', { name: msg.name, no: no.result, priority: '0', productid: msg.productid, create_time });
		const ret = await exec();
		if (ret.code === 1) {
			return 'success';
		} else {
			return 'fail';
		}
	}
}
